//
//  WatchProblemViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/12/11.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "WatchProblemViewController.h"
#import "TestProblemsTableViewCell.h"

@interface WatchProblemViewController ()
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,weak) UIButton *selctButton;
@property (nonatomic,assign) NSInteger slectIndex;

@end

@implementation WatchProblemViewController
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"趣味观察屋";
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TestProblemsTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"TestProblemsTableViewCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    self.slectIndex = 100;
    [self creatDataList];
    [self creatHeadView];
}
#pragma mark - 请求数据
- (void)creatDataList{
    
    for (int i=0; i<3; i++) {
        NSMutableDictionary *replaydic=[NSMutableDictionary dictionary];
        replaydic[@"proContent"]=@"左边";
        replaydic[@"answerSelect"] = i == 0 ? @"A" : @"B";
        replaydic[@"answerContent"] = i != 0 ? @"俩条线一样长，透视的作用俩条线一样长，透视的作用俩条线一样长，透视的作用俩条线一样长，透视的作用":@"俩条线一样长，透视的作用俩条线一样长，透视的作用俩条线一样长，透视的作用俩条线一样长，透视的作用透视的作用俩条线一样长，透视的作用俩条线一样长，透视的作用俩条线一样长，透视的作用透视的作用俩条线一样长，透视的作用俩条线一样长，透视的作用俩条线一样长，透视的作用";
        
    }
}
#pragma mark - creatHeadView
- (void)creatHeadView{
    NSString *proStr = @"眼睛看到的是真的吗？眼睛看到的是真的吗？眼睛看到的是真的吗？眼睛看到的是真的吗？眼睛看到的是真的吗？眼睛看到的是真的吗？\n\n我们不妨来一起猜一猜";
    double proHeight = [Global getSizeOfString:proStr maxWidth:kScreenW - 20 maxHeight:CGFLOAT_MAX withFontSize:15.6].height;
    
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, kScreenW, proHeight + 20 + 20 + (kScreenW - 20)/2 + 40);
    view.backgroundColor = [UIColor whiteColor];
    self.tableView.tableHeaderView = view;
    
    UILabel *proLabel = [[UILabel alloc]init];
    proLabel.frame = CGRectMake(10, 10, kScreenW - 20, proHeight);
    proLabel.font = [UIFont systemFontOfSize:15];
    proLabel.numberOfLines = 0;
    proLabel.text = proStr;
    [view addSubview:proLabel];
    
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.frame = CGRectMake(10, CGRectGetMaxY(proLabel.frame) + 20, kScreenW - 20, (kScreenW - 20)/2);
    imageView.backgroundColor = [UIColor clearColor];
    [view addSubview:imageView];
    
    UILabel *problemLabel = [[UILabel alloc]init];
    problemLabel.text = @"请问：俩条红色的竖线哪个更长呢？";
    problemLabel.frame = CGRectMake(10, CGRectGetMaxY(imageView.frame) + 20, kScreenW - 20, 20);
    [view addSubview:problemLabel];
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section) {
        if (self.slectIndex == 100) {
            return 0;
        }
        return 1;
    }
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section) {
        static NSString *CellIdentifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
            UILabel *desLabel = [[UILabel alloc]init];
            desLabel.tag = 1000;
            desLabel.font = [UIFont systemFontOfSize:15];
            desLabel.textColor = KTEXTMAINCOLOR;
            desLabel.numberOfLines = 0;
            [cell.contentView addSubview:desLabel];
        }
        TestProblemsInfo *testProblemsInfo = self.dataArray[self.slectIndex];
        UILabel *desLabel = [cell.contentView viewWithTag:1000];
        desLabel.frame = CGRectMake(10, 15, kScreenW - 20, [Global getSizeOfString:testProblemsInfo.answerContent maxWidth:kScreenW - 20 maxHeight:CGFLOAT_MAX withFontSize:15.6].height);
        desLabel.text = testProblemsInfo.answerContent;
        return cell;
    }
    TestProblemsTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TestProblemsTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.testProblemsInfo = self.dataArray[indexPath.row];
    __weak typeof (self)unself = self;
    __weak typeof (cell)unCell = cell;
    cell.selectAnswerBlock = ^{
        if (unself.slectIndex != indexPath.row) {
            unCell.selImageView.selected = YES;
            unself.selctButton.selected = NO;
            unself.selctButton = unCell.selImageView;
            
            unself.slectIndex = indexPath.row;
            [unself.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
        }
        
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section) {
        TestProblemsInfo *testProblemsInfo = self.dataArray[self.slectIndex];
        return [Global getSizeOfString:testProblemsInfo.answerContent maxWidth:kScreenW - 20 maxHeight:CGFLOAT_MAX withFontSize:15.6].height + 30;
        //        return 100;
    }
    return 32;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
